﻿namespace DataIntegration.Utils.Collections
{
    using System;
    using System.Collections.Generic;
    using System.Threading;

    public class BlockingQueue<T>
    {
        Queue<T> queue;

        Semaphore semaphore;

        public BlockingQueue()
        {
            this.queue = new Queue<T>();
            this.semaphore = new Semaphore(0, Int32.MaxValue);
        }

        public void Enqueue(T item)
        {
            lock (this.queue)
            {
                this.queue.Enqueue(item);
            }

            this.semaphore.Release();
        }

        public T Dequeue()
        {
            this.semaphore.WaitOne();

            lock (this.queue)
            {
                return this.queue.Dequeue();
            }
        }
    }
}